angular.module('Illust', ['commonApp'])
.run(["$rootScope", ($rootScope)->
        $rootScope.target = ""
    
    ])
.config(['$routeProvider', ($routeProvider) ->
    $routeProvider
    .when '/illust',
        templateUrl: "/public/tpl/illust.html"
        controller: "masonryCtrl"
    .when '/illust/:page',
        templateUrl: "/public/tpl/illust.html"
        controller: "masonryCtrl"
    .when '/illust/tag/:tid',
        templateUrl: "/public/tpl/illust.html"
        controller: "masonryCtrl"
    .when '/illust/tag/:tid/p/:page',
        templateUrl: "/public/tpl/illust.html"
        controller: "masonryCtrl"
    ])
.filter('like',  ->
    (like) ->
        if like
            "icon-heart"
        else
            "icon-heart-empty"
    )
.controller('masonryCtrl', ['$scope','$http', '$timeout', '$routeParams','showMsg', '$location', '$rootScope', 'togFullScreen', ($scope, $http, $timeout, $routeParams,showMsg, $location, $rootScope, togFullScreen) ->
    $scope.full = togFullScreen
    $scope.list = 
        like: 0
    page = Number($routeParams.page) || 1
    $scope.tid = tid = $routeParams.tid
    $scope.like = (id) ->
        Self = @
        $http.get("/illust/like/id/#{id}").success((data)->
            if data is "1" or data is "2"
                # console.log(Self)
                if Self.list.like is '0'
                  Self.hearte = "icon-heart"
                  Self.hearts = "icon-heart-empty"
                  Self.heart = "icon-heart"
                  Self.list.like = 1
                else 
                  Self.hearts = "icon-heart"
                  Self.hearte = "icon-heart-empty"
                  Self.hearts = "icon-heart"
                  Self.list.like = 0
            else
                showMsg "出现了一些错误"
            showMsg(if data is "1" then "已标记为喜欢" else "已取消标记")
            # console.log data
            )
    reload = false
    # if !reload 
    $scope.currentPage = page || 1
    $scope.$watch 'currentPage', (newPage) ->
        # console.log newPage
        
        if newPage != page
            # console.log("new",newPage)
            if tid 
                console.log tid, page, newPage
                $location.path("/illust/tag/#{tid}/p/#{newPage}")

            else
                $location.path("/illust/p/#{newPage}")
    getData = ->
        $http.get("/illust/data/page/#{page}/tag/#{tid}").success((data)->
            
            # console.log(data['img'])
            container = $('#container');
            if data['img']
                
                $scope.lists = data['img']
                if !reload

                    $("#tagid_#{tid}").addClass("active")
                    $timeout(-> 
                        container.imagesLoaded( -> 
                            container.masonry(
                                itemSelector: '.item'
                                isFitWidth:true #自适应宽度
                                # // columnWidth: 100,
                                isAnimated: true
                                isResizableL:true
                                cornerStampSelector: ".coninfo"
                            )
                        )
                    )
                    reload = true
                else
                    $timeout(-> 
                        # container.imagesLoaded( -> 
                            container.masonry().masonry 'reload'
                        # )
                    )
            else
                $scope.lists = false
            $scope.noOfPages = data['count']

            $scope.tags = data['tags']
            $scope.bcs = data['bcs'] || '/'
            # console.log 
            # $scope.currentPage = page
            console.log data
            # container.masonry('reloadItems')
        )
    # getData()
    $rootScope.$watch 'logstatus', (newstatus) ->
        getData()
])